var fs = require("fs");
var path = require("path");
var _ora = require("ora");

var _ora2 = _interopRequireDefault(_ora);
function _interopRequireDefault(obj) {
  return obj && obj.__esModule ? obj : { default: obj };
}

exports.repairIOS = () => {
  return new Promise((resolve) => {
    //调用文件遍历方法
    // 修改文件操作
    var res, res2;
    // /node_modules/react-native/local-cli/runIOS
    var templateName =
      "./node_modules/react-native/local-cli/runIOS/findMatchingSimulator.js";
    var filePath = path.resolve(templateName);
    if (fs.existsSync(filePath)) {
      //根据文件路径获取文件信息，返回一个fs.Stats对象
      let stats = fs.statSync(filePath);
      var isFile = stats.isFile(); //是文件
      if (isFile) {
        res = replaceContent(filePath);
      }
    }

    // /node_modules/react-native/React/Base/RCTModuleMethod.mm
    var templateName2 =
      "./node_modules/react-native/React/Base/RCTModuleMethod.mm";
    var filePath2 = path.resolve(templateName2);
    console.log(filePath2);
    if (fs.existsSync(filePath2)) {
      //根据文件路径获取文件信息，返回一个fs.Stats对象
      let stats2 = fs.statSync(filePath2);
      var isFile2 = stats2.isFile(); //是文件
      if (isFile2) {
        res2 = replaceContent2(filePath2);
      }
    }

    if (res === "get") {
      loading2 = (0, _ora2.default)("");
      loading2.start();
      loading2.succeed(`向上兼容xcode模拟器版本完成`);
    }
    if (res2 === "get") {
      loading2 = (0, _ora2.default)("");
      loading2.start();
      loading2.succeed(`xcode 11兼容完成`);
    }
    if (res === "get" && res2 === "get") {
      resolve("get");
    } else {
      resolve("error");
    }
  });
};

function replaceContent2(filedir) {
  let files = fs.readFileSync(filedir, "utf8");
  try {
    if (files.indexOf("__attribute__((__unused__))") != -1) {
      return "get";
    }
    var result = files.replace(
      new RegExp(/static BOOL RCTParseUnused([^\}]*)/, "ig"),
      "static BOOL RCTParseUnused(const char **input){\n" +
        'return RCTReadString(input, "__unused") ||\n\t' +
        'RCTReadString(input, "__attribute__((__unused__))") ||\n\t' +
        'RCTReadString(input, "__attribute__((unused))");\n'
    );

    if (result && result.length > 0) {
      fs.writeFileSync(filedir, result, "utf8");
    }
    return "get";
  } catch (e) {
    console.log("fail:", filedir);
    return "error";
  }
}

function replaceContent(filedir) {
  let files = fs.readFileSync(filedir, "utf8");
  // result = files.replace(
  //   new RegExp(/simulator.availability([^\}]*)/, "ig"),
  //   " simulator.isAvailable !== true && simulator.availability !== '(available)' &&\n\t" +
  //     "simulator.isAvailable !== 'YES'\n\t" +
  //     ") {\n\t\t" +
  //     " continue;\n\t" +
  //     "}\n\t" +
  //     "if (simulator.name==='iPhone 11 Pro'){\n\t" +
  //     " temp=simulator;\n\t" +
  //     " temp.version=version;\n\t"
  // );
  // result = result.replace(
  //   new RegExp(/if(.*)\(match\)([^\}]*)/, "ig"),
  //   " if (match) {\n\t" +
  //     "  return match;\n" +
  //     "  }else{\n\t" +
  //     " // 不可用 使用一个可用的进行返回 \n\t" +
  //     "if(temp){\n\t\t" +
  //     " return {\n\t\t\t" +
  //     " udid: temp.udid,\n\t\t\t" +
  //     " name: temp.name,\n\t\t\t" +
  //     " booted='Booted',\n\t\t\t" +
  //     " version:temp.version,\n\t\t\t" +
  //     " };\n\t\t" +
  //     "}\n\t"
  // );

  // console.log(result);
  try {
    if (
      files.indexOf(
        "!version.startsWith('iOS')&&!version.startsWith('com.apple.CoreSimulator.SimRuntime.iOS') &&!version.startsWith('com.apple.CoreSimulator.SimRuntime.tvOS')"
      ) != -1
    ) {
      return "get";
    }
    let result = files.replace(
      new RegExp(/!version.startsWith\('iOS'\)/, "ig"),
      "!version.startsWith('iOS')&&!version.startsWith('com.apple.CoreSimulator.SimRuntime.iOS') &&!version.startsWith('com.apple.CoreSimulator.SimRuntime.tvOS')"
    );
    result = result.replace(
      new RegExp(/var match;/, "ig"),
      "var match;\n  var temp;"
    );
    result = result.replace(
      new RegExp(/simulator.availability([^\}]*)/, "ig"),
      " simulator.isAvailable !== true && simulator.availability !== '(available)' &&\n\t" +
        "simulator.isAvailable !== 'YES'\n\t" +
        ") {\n\t\t" +
        " continue;\n\t" +
        "}\n\t" +
        "if (simulator.name==='iPhone 11 Pro'){\n\t" +
        " temp=simulator;\n\t" +
        " temp.version=version;\n\t"
    );
    result = result.replace(
      new RegExp(/if(.*)\(match\)([^\}]*)/, "ig"),
      " if (match) {\n\t" +
        "  return match;\n" +
        "  }else{\n\t" +
        " // 不可用 使用一个可用的进行返回 \n\t" +
        "if(temp){\n\t\t" +
        " return {\n\t\t\t" +
        " udid: temp.udid,\n\t\t\t" +
        " name: temp.name,\n\t\t\t" +
        " booted:'Booted',\n\t\t\t" +
        " version:temp.version,\n\t\t\t" +
        " };\n\t\t" +
        "}\n\t"
    );

    if (result && result.length > 0) {
      fs.writeFileSync(filedir, result, "utf8");
    }

    return "get";
  } catch (e) {
    console.log("fail:", filedir);
    return "error";
  }
}
